package com.console.operations.planmgr;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.console.pages.planmgr.FinancePlanLoanCreditPage;

	/**
		@author 王嘉犇
		@name 钜宝盆后台理财计划债权放款
		@type 动作类
	 */

public class FinancePlanLoanCredit extends OperationBase {

	public FinancePlanLoanCredit(UITestBase tb){
		super(tb);
	}
	
	//判断债券状态是否变为13(放款银行处理中)
	public boolean isloanstatuscorrected = false;
	
	public void credit() throws Exception{
		switchToDefaultContent();
		FinancePlanLoanCreditPage fplp = new FinancePlanLoanCreditPage(tb);
		waitForElementDisplayed(fplp.titlediv);
		elementToBeSelected(fplp.titlediv);
		//得到最早一条满标债权的序列号和债权名称
		Map<String, String> rowmap = DBUtil.queryOneRow("select l.vc_sequence sequence, l.vc_name loanname, l.nb_amount nbamount, l.nb_rate nbrate\n"
				   									  + "from fiz_loan l\n"
				   									  + "where l.dc_status = ?\n"
				   									  + "  and l.dc_type = ?\n"
				   									  + "  and l.pk_id in (\n"
				   									  + "				   select fk_loan_id\n"
				   									  + "				   from fiz_loan_prop\n"
				   									  + "				   where vc_code = 'direct'\n"
				   									  + "				     and vc_value = '01'\n"
				   									  + "			      )\n"
				   									  + "order by l.vc_sequence asc limit 1", new Object[]{"11", "03"});
		//找到状态为满标并且创建时间最早的一条债券序列号
		String sequence = rowmap.get("sequence");
		log.info("债权序列号为  " + sequence);
		//找到状态为满标并且创建时间最早的一条债券名称
		String financeplanloanname = rowmap.get("loanname");
		log.info("理财计划债权名称为  " + financeplanloanname);
		//得到最早一条债权的限额
		String nbamount = rowmap.get("nbamount");
		//债权限额变成逗号分隔的格式
		String havecommaformatnbamount = havacommanumformat(nbamount);
		log.info("理财计划债权限额的逗号显示格式为  " + havecommaformatnbamount);
		//得到最早一条债权的年化利率
		String nbrate = rowmap.get("nbrate");
		String percentformatnbrate = pencentformat(nbrate);
		log.info("理财计划债权年化利率百分号显示格式为 " + percentformatnbrate);
		//得到放款页面的分页页数
		int pagecount = Integer.parseInt(fplp.lastpageLink.getText());
		log.info("放款页的页数为 " + pagecount);
		//设置一个是否找到债权的标志位
		boolean isfound = false;
		//定位序列号控件的详情链接的findValue
		fplp.detailsLnk.setfindValue("//tr[td[text()='" + sequence + "'] and td[text()='" + financeplanloanname + "'] and td[text()='" + 
									 havecommaformatnbamount + "'] and td[text()='" + percentformatnbrate + "']]/td[a[text()='详情']]/a");
		if(pagecount==1){
			log.info("不存在分页的情况");
			if(isPresent(fplp.detailsLnk)){
				isfound = true;
			}
		}else{
			log.info("存在分页的情况");
			//分页查找是有有对应债权序列号的
			for(int i=1;i<=pagecount;i++){
				fplp.presentpageLinkli.setfindName("xpath");
				fplp.presentpageLinkli.setfindValue("//ul[@class='pagination']/li[a[text()='" + i + "']]");
				fplp.presentpageLink.setfindName("linkText");
				fplp.presentpageLink.setfindValue(String.valueOf(i));
				waitForElementDisplayed(fplp.presentpageLinkli);
				if(i!=1){
					fplp.presentpageLink.click();
					log.info("点击第" + i + "页链接");
				}
				waitForElementelementToBeClickable(fplp.presentpageLinkli);
				waitForPageLinkclicked(fplp.presentpageLinkli);
				if(isPresent(fplp.detailsLnk)){
					isfound = true;
					break;
				}
			}
		}
		if(!isfound){
			handlefailure("页面上找不到该债权序列号");
		}
		fplp.detailsLnk.click();
		waitForElementDisplayed(fplp.lendButton);
		//点击放款按钮
		fplp.lendButton.click();
		//直到确定按钮出现
		waitForElementExist(fplp.assureButton);
		//直到确定按钮可视
		waitForElementDisplayed(fplp.assureButton);
		//点击确定按钮
		fplp.assureButton.click();
		/*
		 * 点击放款后，应该生成债权的还款计划
		 */
		waitForPageContainsStr("已成功放款");
		String status = DBUtil.queryOneCell("select dc_status\n" +
											"from fiz_loan\n" +
											"where vc_sequence = ?", new Object[]{sequence});
		//判断债券状态是否为还款中
		isloanstatuscorrected = status.equals("13");
		if(!isloanstatuscorrected){
			log.error("债权的状态不为13(放款银行处理中)");
			return;
		}
		DBUtil.update("update fiz_transaction_record\n"
					+ "set dc_status = ?\n"
					+ "where dc_type = ?\n"
					+ "  and vc_relation_id = (\n"
					+ "                        select pk_id\n"
					+ "                        from fiz_loan\n"
					+ "                        where vc_sequence = ?\n"
					+ "                       )", new Object[]{"20", "0070", sequence});
	}
	
	/**
	 * 逗号分隔显示数字格式
	 */
	private String havacommanumformat(String numstr) {
		BigDecimal numstrbd = new BigDecimal(numstr).setScale(2, BigDecimal.ROUND_DOWN);
        DecimalFormat df = new DecimalFormat("#,###.00"); 
        return df.format(numstrbd.doubleValue());
    }
	
	/**
	 * 数字加百分号格式
	 */
	private String pencentformat(String numstr){
		BigDecimal numstrbd = new BigDecimal(numstr).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_DOWN);
		return numstrbd.toString() + "%";
	}
}
